#include<bits/stdc++.h>
#define int long long 
using namespace std;

inline int read() {
	int x = 0, f = 0; char ch = getchar();
	while(ch < '0' or ch > '9') f |= (ch == '-'), ch = getchar();
	while(ch >= '0' and ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}
int __stk[128], __top;
inline void write(int x) {
    if(x < 0) putchar('-'), x = -x;
	do { __stk[++__top] = x % 10, x /= 10; } while(x);
	while (__top) putchar(__stk[__top--] + '0');
}
bool stmer;
const int N = 500;
int n, ans;
int a[N];
vector<int> e[N];
namespace sub1 {
    int res = 0;
    bool vis[N];
    void dfs(int x) {
        if(x == n) return ans = max(ans, res), void();
        for(int i = 0; i < n; i++) if(!vis[i]) {
            for(int v : e[i]) if(!vis[v]) a[v] += a[i];
            vis[i] = 1, res += a[i], dfs(x + 1), res -= a[i], vis[i] = 0;
            for(int v : e[i]) if(!vis[v]) a[v] -= a[i];
        }
    }
    void solve() {
        dfs(0), write(ans);
    }
}
namespace sub2 {
    bool check() {
        return e[0].size() >= n - 1;
    }
    void solve() {
        sort(a + 1, a + n), ans = (n - 1) * a[0];
        for(int i = n - 1, sum = a[0]; i; i--) {
            if(a[i] * (i - 1) - sum > 0) ans += a[i] * i - sum, sum += a[i];
            else break;
        }
        for(int i = 0; i < n; i++) ans += a[i];
        write(ans);
    }
}
bool edmer;
signed main() {
	freopen("light.in", "r", stdin);
	freopen("light.out", "w", stdout);
	cerr << "[Memory] " << (&stmer - &edmer) / 1024 / 1024 << " MB\n";
	n = read();
    for(int i = 0; i < n; i++) a[i] = read();
    for(int i = 1; i < n; i++) {
        int u = read() - 1, v = read() - 1;
        e[u].push_back(v), e[v].push_back(u);
    }
    if(n <= 10) sub1 :: solve();
    else if(sub2 :: check()) sub2 :: solve();
    cerr << "[Runtime] " << (double) clock() / CLOCKS_PER_SEC << " seconds";
	return 0;
} 